package org.dromara.iot.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.iot.domain.bo.IotXfclDeviceBindBo;
import org.dromara.iot.domain.vo.IotXfclDeviceBindVo;
import org.dromara.iot.service.IIotXfclDeviceBindService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车辆与设备绑定关系
 *
 * @author Luo
 * @date 2025-07-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/xfclDeviceBind")
public class IotXfclDeviceBindController extends BaseController {

    private final IIotXfclDeviceBindService iotXfclDeviceBindService;

    /**
     * 查询车辆与设备绑定关系列表
     */
    @SaCheckPermission("iot:xfclDeviceBind:list")
    @GetMapping("/list")
    public TableDataInfo<IotXfclDeviceBindVo> list(IotXfclDeviceBindBo bo, PageQuery pageQuery) {
        return iotXfclDeviceBindService.queryPageList(bo, pageQuery);
    }

    /**
     * 分页查询车辆及对应设备信息
     */
    @SaCheckPermission("iot:xfclDeviceBind:list")
    @GetMapping("/pageWithDevice")
    public TableDataInfo<IotXfclDeviceBindVo> listWithDevice(IotXfclDeviceBindBo bo, PageQuery pageQuery) {
        return iotXfclDeviceBindService.queryXfclWithDevicePage(bo, pageQuery);
    }

    /**
     * 导出车辆与设备绑定关系列表
     */
    @SaCheckPermission("iot:xfclDeviceBind:export")
    @Log(title = "车辆与设备绑定关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IotXfclDeviceBindBo bo, HttpServletResponse response) {
        List<IotXfclDeviceBindVo> list = iotXfclDeviceBindService.queryList(bo);
        ExcelUtil.exportExcel(list, "车辆与设备绑定关系", IotXfclDeviceBindVo.class, response);
    }

    /**
     * 获取车辆与设备绑定关系详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("iot:xfclDeviceBind:query")
    @GetMapping("/{id}")
    public R<IotXfclDeviceBindVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iotXfclDeviceBindService.queryById(id));
    }

    /**
     * 新增车辆与设备绑定关系
     */
    @SaCheckPermission("iot:xfclDeviceBind:add")
    @Log(title = "车辆与设备绑定关系", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IotXfclDeviceBindBo bo) {
        return toAjax(iotXfclDeviceBindService.insertByBo(bo));
    }

    /**
     * 修改车辆与设备绑定关系
     */
    @SaCheckPermission("iot:xfclDeviceBind:edit")
    @Log(title = "车辆与设备绑定关系", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotXfclDeviceBindBo bo) {
        return toAjax(iotXfclDeviceBindService.updateByBo(bo));
    }

    /**
     * 删除车辆与设备绑定关系
     *
     * @param ids 主键串
     */
    @SaCheckPermission("iot:xfclDeviceBind:remove")
    @Log(title = "车辆与设备绑定关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iotXfclDeviceBindService.deleteWithValidByIds(List.of(ids), true));
    }
}
